package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.pojo.Receive;
import com.shopping.util.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ReceiveController {

    @Autowired
    com.shopping.service.receive.ReceiveServiceImpl receiveService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = {"/receive/addReceiver"})
    public Map<String, Object> addReceiver(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Receive receive = new Receive();
        receive.setReceiveName(jsonObject.getString("receiveName"));
        receive.setReceiveAddress(jsonObject.getString("receiveAddress"));
        receive.setPostcode(jsonObject.getString("postcode"));
        receive.setPhone(jsonObject.getString("phone"));
        receive.setDefault(jsonObject.getBoolean("isDefault"));
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(receive)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
            return map;
        }
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        receive.setId(redisUserId);
        return receiveService.addReceiver(receive);
    }

    @PostMapping(value = {"/receive/deleteReceiver"})
    public Map<String, Object> deleteReceiver(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int receiverId = Integer.parseInt(jsonObject.getString("receiverId"));
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        Map<String, Object> result = receiveService.selectUserIdById(receiverId);
        if(result.get("code").equals(ResponseCode.REQUEST_SUCCESSED.getValue())){
            if(Integer.parseInt(String.valueOf(result.get("userId"))) == redisUserId){
                return receiveService.deleteReceiver(receiverId);
            }
        }
        map.put("code", ResponseCode.SERVER_ERROR.getValue());
        return map;
    }

    @PostMapping(value = {"/receive/showAllReceivers"})
    public Map<String, Object> showAllReceivers(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return receiveService.showAllReceivers(redisUserId);
    }

    @PostMapping(value = {"/receive/showOneReceiver"})
    public Map<String, Object> showOneReceiver(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        int id = Integer.parseInt(jsonObject.getString("id"));
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        Map<String, Object> result = receiveService.selectUserIdById(id);
        if(result.get("code").equals(ResponseCode.REQUEST_SUCCESSED.getValue())){
            if(Integer.parseInt(String.valueOf(result.get("userId"))) == redisUserId){
                return receiveService.showOneReceiver(id);
            }
        }
        map.put("code", ResponseCode.SERVER_ERROR.getValue());
        return map;
    }

    @PostMapping(value = {"/receive/modifyReceiver"})
    public Map<String, Object> modifyReceiver(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Receive receive = new Receive();
        receive.setReceiveName(jsonObject.getString("receiveName"));
        receive.setReceiveAddress(jsonObject.getString("receiveAddress"));
        receive.setPostcode(jsonObject.getString("postcode"));
        receive.setPhone(jsonObject.getString("phone"));
        receive.setDefault(jsonObject.getBoolean("isDefault"));
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(receive)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
            return map;
        }
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        receive.setId(redisUserId);
        return receiveService.modifyReceiver(receive);
    }
}
